package com.gec.hawaste.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.hawaste.entity.AppVersion;
import com.gec.hawaste.service.IAppVersionService;
import com.gec.hawaste.utils.PageInfo;
import com.gec.hawaste.utils.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenxys
 * @since 2022-07-01
 */
@Slf4j
@RestController
@RequestMapping("/manager/app")
public class AppVersionController {

    @Autowired
    private IAppVersionService appVersionService;

    /**
     * 分页查询
     * @author chenxys
     * @param current 当前页
     * @param size 每页显示条数
     * @return com.gec.hawaste.utils.ResultBean<com.gec.hawaste.utils.PageInfo>
     */
    @GetMapping("/select")
    public ResultBean<PageInfo> select(Long current, Long size){
        PageInfo<AppVersion> page = new PageInfo<AppVersion>(current,size);
        page = appVersionService.page(page);

        // 设置显示的页码数
        page.setNavigatePage();
        log.info("总页数:{}",page.getPages());
        log.info("app进行分页版本查询");
        return ResultBean.ok(page);
    }

    /**
     * 根据ID查询app
     * @author chenxys
     * @param id appID
     * @return com.gec.hawaste.utils.ResultBean<com.gec.hawaste.entity.AppVersion>
     */
    @GetMapping("/selectOne")
    public ResultBean<AppVersion> selectOne(Long id){
        //Long appId = Long.parseLong(id);

        AppVersion appVersion = appVersionService.getById(id);
        return ResultBean.ok(appVersion);
    }

    /**
     * 保存或更新
     * @author chenxys
     * @param appVersion app信息
     * @return com.gec.hawaste.utils.ResultBean
     */
    @PostMapping("/saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody AppVersion appVersion){
        appVersionService.saveOrUpdate(appVersion);
        log.info("保存或更新app,提交的app信息{}",appVersion);
        return ResultBean.ok();
    }

    /**
     * 根据ID删除
     * @author chenxys
     * @param id app ID
     * @return com.gec.hawaste.utils.ResultBean
     */
    @GetMapping("/delete")
    public ResultBean delete(Long id){
        appVersionService.removeById(id);
        log.info("根据ID删除");
        return ResultBean.ok();
    }

}
